#!/usr/bin/python
# -*- coding: utf-8 -*-


import pygame
import random
import yaml
import io
import logging
import math

from robot import *
from world import *

from pygame.locals import *

logging.basicConfig(level=logging.INFO)


    
 
class App:
    def __init__(self):
        self._running = True
        self._display_surf = None
        self._image_surf = None
        self.world=World()
        self.robot=Robot(self.world,(1,1,0),(0.5,0.3),(255,0,0),0.3,0.5)
        self.lastTime=float(pygame.time.get_ticks()) 
 
    def on_init(self):
        pygame.init()
        self._display_surf = pygame.display.set_mode((600,600), pygame.HWSURFACE)

        self.tempSurface=pygame.Surface((600,600),pygame.HWSURFACE) 
        self._running = True
        #iself._image_surf = pygame.image.load("myimage.jpg").convert()

        #self.particleList=[Particle(200,200),Particle(250,100)]
 
    def on_event(self, event):
        if event.type == QUIT:
            self._running = False

        if event.type==KEYDOWN:
           #print event.key  
           if event.key==273:
              self.robot.update(0.5,0.5)
           elif event.key==275:
              self.robot.update(0.3,-0.3)
           elif event.key==276:
              self.robot.update(-0.3,0.3)
           elif event.key==274:
              self.robot.update(-0.5,-0.5)  
        elif event.type==MOUSEBUTTONDOWN:
             self.robot.pose=(event.pos[0]*self.world.resolution,event.pos[1]*self.world.resolution,0) 
 
                 
        #print event

    def on_loop(self):
        pass
      
        #self.robot.update(0.1,0.1)

    def on_render(self):
        #self._display_surf.blit(self._image_surf,(0,0))
        pygame.draw.line(self._display_surf,(255,255,255),(0,0),(100,100)) 
        self.world.draw(self.tempSurface) 
        self.robot.draw(self.tempSurface)
        #temp=pygame.transform.rotate(self.tempSurface,90) 
        self._display_surf.blit(self.tempSurface,(0,0))
        pygame.display.flip()
 
    def on_cleanup(self):
        pygame.quit()
 
    def on_execute(self):
        if self.on_init() == False:
            self._running = False
 
        while( self._running ):
            for event in pygame.event.get():
                self.on_event(event)
            self.on_loop()
            self.on_render()
        self.on_cleanup()
 
if __name__ == "__main__" :
    theApp = App()
    theApp.on_execute()






